package com.uw.scenic.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uw.scenic.entity.SysUser;
import com.uw.scenic.service.TestServcie;
import com.uw.scenic.vo.R;
import com.uw.scenic.dto.UserDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 案例：
 * 测试基本的crud操作
 *
 *  增删改查的案例
 * @author yls
 * @date 2021-03-02
 *
 */
@Slf4j
@RestController
@RequestMapping("/test")
@Api(value = "test", tags = "测试基本的curd操作")
public class TestController {

    @Autowired
    private TestServcie userService;


    @ApiOperation(value = "获取详情",notes = "获取详情",httpMethod = "GET")
    @GetMapping("/getById")
    public R<SysUser> getById(@ApiParam(name="id",value="主键id",required=true) @RequestParam  Long id) {
        log.info("get1={}", id);
        SysUser user = userService.findById(id);
        return R.success(user);
    }

    @ApiOperation(value = "分页查询接口",notes = "分页查询接口",httpMethod = "GET")
    @GetMapping("/getPageList")
    public R<IPage> getPageList(@ApiParam(name = "page", value = "查询下一页的第几页", required = false) @RequestParam Integer page,
                                @ApiParam(name = "pageSize", value = "分页的每一页显示的条数", required = false)
                                @RequestParam Integer pageSize) {
        if (page == null) {
            page = 1;
        }

        if (pageSize == null) {
            pageSize = 10;
        }
        Page pageInfo = userService.getPageList(page,pageSize);
        return R.success(pageInfo);
    }


    @ApiOperation(value = "保存接口",notes = "保存接口",httpMethod = "POST")
    @PostMapping("/save")
    public R<SysUser> save(@RequestBody UserDTO userDTO) {
        log.info("get1={}", userDTO);
        int rs = userService.save(userDTO);
        if(rs<0){
            return R.fail("保存失败了");
        }
        return R.successDef();
    }

    @ApiOperation(value = "更新接口",notes = "更新接口",httpMethod = "POST")
    @PostMapping("/update")
    public R<SysUser> update(@RequestBody UserDTO userDTO) {
        if(userDTO.getId()==null){
            return R.fail("主键id为空了");
        }
        log.info("get1={}", userDTO);
        int rs  = userService.update(userDTO);
        if(rs<0){
            return R.fail("更新失败");
        }
        return R.successDef();
    }



    @ApiOperation(value = "删除接口",notes = "删除接口",httpMethod = "POST")
    @PostMapping("/delete")
    public R<SysUser> delete(@ApiParam(name="id",value="主键id",required=true) @RequestParam  Long id) {
        log.info("get1={}", id);
        int rs = userService.delete(id);
        if(rs<0){
            return R.fail("更新失败");
        }
        return R.successDef();
    }


}
